package com.yy.wga.business.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.yy.wga.mp.common.interceptor.RequestUserHolder;
import com.yy.wga.mp.common.utils.common.StringUtils;
import com.yy.wga.mp.common.utils.mapping.BeansUtils;
import com.yy.wga.mp.module.admin.request.AdminBusinessCoursePageRequest;
import com.yy.wga.mp.module.admin.response.AdminBusinessCoursePageResponse;
import com.yy.wga.mp.module.business.entity.pojo.BusinessCourseDO;
import com.yy.wga.mp.module.business.mapper.BusinessCourseMapper;
import com.yy.wga.mp.module.business.response.BusinessCourseInfoResponse;
import com.yy.wga.mp.module.business.response.BusinessCourseVideoInfoResponse;
import com.yy.wga.mp.module.business.service.BusinessCourseService;
import com.yy.wga.mp.module.business.service.BusinessCourseVideoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商学院课程信息(BusinessCourse)表服务实现类
 *
 * @author guochao
 * @since 2025-02-09 15:18:37
 */
@Service
@RequiredArgsConstructor
public class BusinessCourseServiceImpl extends ServiceImpl<BusinessCourseMapper, BusinessCourseDO> implements BusinessCourseService {

    private final BusinessCourseVideoService courseVideoService;

    @Override
    public PageBean<AdminBusinessCoursePageResponse> pageData(AdminBusinessCoursePageRequest request) {
        LambdaQueryWrapper<BusinessCourseDO> queryWrapper = new LambdaQueryWrapper<BusinessCourseDO>()
                .eq(BusinessCourseDO::getAppKey, RequestUserHolder.getBaseRequest().getAppKey())
                .like(StringUtils.isNotBlank(request.getName()), BusinessCourseDO::getName, request.getName());
        if (request.getStatus() != null) {
            // 状态  0：未开始 1：已上架 2：已下架
            if (request.getStatus() == 0) {
                queryWrapper.ne(BusinessCourseDO::getStatus, 2)
                        .ge(BusinessCourseDO::getStartTime, new Date());
            } else if (request.getStatus() == 1) {
                Date now = new Date();
                queryWrapper.ne(BusinessCourseDO::getStatus, 2)
                        .le(BusinessCourseDO::getStartTime, now)
                        .ge(BusinessCourseDO::getEndTime, now);
            } else {
                // status = 2 or endTime < now
                queryWrapper.and(wrapper -> wrapper.eq(BusinessCourseDO::getStatus, 2)
                        .or(query -> query.lt(BusinessCourseDO::getEndTime, new Date())));
            }
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<BusinessCourseDO> courseList = baseMapper.selectList(queryWrapper);
        Date now = new Date();
        for (BusinessCourseDO course : courseList) {
            if (course.getStatus() == 2) {
                continue;
            }
            if (now.before(course.getStartTime())) {
                course.setStatus(0);
            } else if (now.after(course.getEndTime())) {
                course.setStatus(2);
            } else {
                course.setStatus(1);
            }
        }
        PageBean<AdminBusinessCoursePageResponse> pageBean = new PageBean<>();
        pageBean.setPageInfo(courseList);
        pageBean.setList(BeansUtils.mapList(courseList, AdminBusinessCoursePageResponse.class));
        return pageBean;
    }

    @Override
    public List<BusinessCourseInfoResponse> listData() {
        Date now = new Date();
        List<BusinessCourseDO> courseList = baseMapper.selectList(new LambdaQueryWrapper<BusinessCourseDO>()
                .eq(BusinessCourseDO::getAppKey, RequestUserHolder.getBaseRequest().getAppKey())
                .lt(BusinessCourseDO::getStartTime, now)
                .gt(BusinessCourseDO::getEndTime, now)
                .in(BusinessCourseDO::getStatus, Arrays.asList(0, 1)));
        if (CollectionUtils.isEmpty(courseList)) {
            return Collections.emptyList();
        }
        List<Long> courseIds = courseList.stream().map(BusinessCourseDO::getId).collect(Collectors.toList());
        List<BusinessCourseVideoInfoResponse> videoInfoResponseList = courseVideoService.list(courseIds);
        Map<Long, List<BusinessCourseVideoInfoResponse>> courseVideoMap = videoInfoResponseList.stream()
                .collect(Collectors.groupingBy(BusinessCourseVideoInfoResponse::getCourseId));

        List<BusinessCourseInfoResponse> responseList = BeansUtils.mapList(courseList, BusinessCourseInfoResponse.class);
        for (BusinessCourseInfoResponse response : responseList) {
            List<BusinessCourseVideoInfoResponse> videos = courseVideoMap.getOrDefault(response.getId(), Collections.emptyList());
            response.setVideos(videos);
        }
        return responseList;
    }
}
